package io.unicorn.modules.water.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.unicorn.modules.water.dao.TDevicesTypeDao;
import io.unicorn.modules.water.entity.TAccountManageEntity;
import io.unicorn.modules.water.entity.TDevicesTypeEntity;
import io.unicorn.modules.water.entity.TModeTypeEntity;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.unicorn.common.utils.PageUtils;
import io.unicorn.common.utils.Query;

import io.unicorn.modules.water.dao.TDeviceInfoDao;
import io.unicorn.modules.water.entity.TDeviceInfoEntity;
import io.unicorn.modules.water.service.TDeviceInfoService;


@Service("tDeviceInfoService")
public class TDeviceInfoServiceImpl extends ServiceImpl<TDeviceInfoDao, TDeviceInfoEntity> implements TDeviceInfoService {
    @Autowired
    TDevicesTypeDao tDevicesTypeDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String deptId = (String)params.get("dept");

        String key   = (String)params.get("key");
        String value = (String)params.get("value");

        IPage<TDeviceInfoEntity> page = this.page(
                new Query<TDeviceInfoEntity>().getPage(params),
                new QueryWrapper<TDeviceInfoEntity>()
                        .like(StringUtils.isNotBlank(value) && StringUtils.isNotBlank(key), key, value)
                        .like(StringUtils.isNotBlank(deptId), "nei_name", deptId)

        );

        //填充设备类型
        Set<Integer> typeIds = page.getRecords().stream().map(TDeviceInfoEntity::getTypeId).collect(Collectors.toSet());
        if (typeIds.size()>0) {
            List<TDevicesTypeEntity> typeList = tDevicesTypeDao.selectList(Wrappers.lambdaQuery(TDevicesTypeEntity.class).in(TDevicesTypeEntity::getTypeId, typeIds));
            Map<Integer, String> hashMap = typeList.stream().collect(Collectors.toMap(TDevicesTypeEntity::getTypeId, TDevicesTypeEntity::getTypeName));
            page.getRecords().forEach(e -> e.setTypeName(hashMap.get(e.getTypeId())));
        }

        return new PageUtils(page);
    }

}
